package org.arain.baseii.upload;

import javax.servlet.http.HttpServletRequest;

/**
 * ueditor工具类
 * 
 * @author linjw
 *
 */
public class SystemUtil {
	/**
	 * 附件服务器地址
	 */
	public static String  UPLOAD_APP_URL = "http://127.0.0.1:8082";
	
	/**
	 * 上传服务器项目名称
	 */
	public static String  UPLOAD_APP_NANME = "";
	
	/**
	 * 附件服务器项目名称
	 */
	public static String ATTACHMENTS_APP_NAME = "/base/images/";
	
	/**
	 * 获取TOMCAT下文件服务保存地址
	 * @return
	 */
	public static String getTomcatPath(){
		StringBuffer path = new StringBuffer(); 
		path.append(System.getProperty("catalina.home").replace( "\\", "/" ))
			.append("/webapps")
			.append(ATTACHMENTS_APP_NAME);
		return path.toString();
	}
	
	public static String getLocalPath(HttpServletRequest request) {
		StringBuffer url = new StringBuffer(); 
		url.append(request.getSession().getServletContext().getRealPath(""))
		.append(ATTACHMENTS_APP_NAME);
		return url.toString();
	}
	
	
	public static String getAttachmentsPath(String attachmentType, String suffix) {
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(getAttachmentsTypePath(attachmentType));//类型目录
		stringBuffer.append(getAttachmentsFormatPath());//后目录
		stringBuffer.append(suffix);//后缀名
		return stringBuffer.toString();
	}
	
	private static String getAttachmentsFormatPath(){
		return AttachmentsPathFormatUtil.parse(CommonContains.ATTACHMENTS_PATH_FARMAT);
	}
	
	/**
	 * 获取目录中部
	 */
	private static String getAttachmentsTypePath(String attachmentType){
		if ("1".equals(attachmentType)) {// webuoloader
			return CommonContains.ATTACHMENTS_NEWS_PATH;
		} else if ("2".equals(attachmentType)) {// ueditor
			return CommonContains.ATTACHMENTS_PRODUCT_PATH;
		} else {
			return "/other";
		}
	}
	
}
